/**
 * 
 */
package export;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * This class provides a method to export optimization data into a latex table.
 * 
 * @author Kevin Wagner
 * @version 1.0
 * 
 */
public class TEXExport {

	private static String sep = System.getProperty("line.separator");

	/**
	 * Exports the given result to the file result.txt.
	 * 
	 * @param result
	 * @param postComma
	 *            post comma digits
	 * @throws Exception
	 */
	public static void exportResult(ArrayList<double[]> result, int postComma)
			throws Exception {
		// Create file
		FileWriter fstream = new FileWriter("result.tex");
		BufferedWriter out = new BufferedWriter(fstream);
		String format = "##0.";
		for (int i = 0; i < postComma; i++) {
			format += "0";
		}
		DecimalFormat df = new DecimalFormat(format);
		out.write("\\tablecaption{toset}" + sep);
		out.write("\\label{toSet}" + sep);
		out.write("\\begin{supertabular}{|l|l|l|l|l|}" + sep);
		out.write("\\hline" + sep);
		out.write("Gen & Best & Worst & Mean & Std\\\\");
		out.write("\\hline" + sep);
		out.write("\\hline" + sep);
		for (int i = 0; i < result.size(); i++) {
			out.write(i + "&" + df.format(result.get(i)[0]) + "&"
					+ df.format(result.get(i)[1]) + "&"
					+ df.format(result.get(i)[2]) + "&"
					+ df.format(result.get(i)[3]) +"\\\\"+ sep);
			out.write("\\hline" + sep);
		}
		out.write("\\end{supertabular}" + sep);
		out.flush();
		// Close the output stream
		out.close();
	}
}
